$(function () {
    $(".navChoose").removeClass("navChoose");
    $("#introduce").addClass("navChoose");
    //getData($("#productId").val()); //获取存储的经纬度范围、先注释掉不用
    //init();
    sliderChange();

    laydate.render({
        elem: '#beginDate'
        , showBottom: false
        , format: 'yyyy/MM/dd'
        , min: 0
        , max: 365
    });

    laydate.render({
        elem: '#endDate'
        , showBottom: false
        , format: 'yyyy/MM/dd'
        , min: 0
        , max: 365
    });
})

function init() {
    //sliderInit
    $('#longSlider').jRange({
        from: -180,
        to: 180,
        step: 1,
        format: '%s',
        width: 300,
        showLabels: true,
        isRange: true
    });

    $('#latiSlider').jRange({
        from: -90,
        to: 90,
        step: 1,
        format: '%s',
        width: 300,
        showLabels: true,
        isRange: true
    });
    $('#depthSlider').jRange({
        from: -90,
        to: 90,
        step: 1,
        format: '%s',
        width: 300,
        showLabels: true,
        isRange: true
    });
};

function cover(flag) {
    var fileType = $("#fileType").val();
    if (flag == "some" && fileType.toLowerCase() != "nc") {
        $.modal.alertError("非NC格式文件不能选择部分覆盖。")
        return;
    }
    if (flag == "all") {
        $("#all").attr("class", "bc_xz2 left");
        $("#some").attr("class", "bc_xz1 left");
        $("#someDiv").css("display", "none");
        // $("#depthDiv").css("display", "none");
        $("#workType").val("0");
    } else if (flag == "some") {
        $("#all").attr("class", "bc_xz1 left");
        $("#some").attr("class", "bc_xz2 left");
        $("#someDiv").css("display", "block");
        // $("#depthDiv").css("display", "block");
        $("#workType").val("1");
    }
}

var elementsdb = [];

/*function getData(productId) {
    $.ajax({
        type: "post",
        async: true,
        url: "/home/productBrowsing/getSubscribeData",
        data: {
            "productId": productId
        },
        dataType: "json",        //返回数据形式为json
        success: function (res) {
            elementsdb = [];
            var result = res.data
            var data = result.param;
            for (var i = 0; i < data.length; i++) {
                var value = data[i];
                if (value.isOutParameter == 0 && value.elementZh == "经度") {
                    var range = value.minValue + "," + value.maxValue;
                    $('#longSlider').jRange('updateRange', range, range);
                    $('#longDiv').css("display", "");
                    $('#longRange').text("取值范围：" + value.minValue + "~" + value.maxValue);
                    $("#longMinVal").val(value.minValue);
                    $("#longMaxVal").val(value.maxValue);
                    elementsdb.push(value.elementZh + ";" + value.elementEn);
                }
                if (value.isOutParameter == 0 && value.elementZh == "纬度") {
                    var range = value.minValue + "," + value.maxValue;
                    $('#latiSlider').jRange('updateRange', range, range);
                    $('#latiDiv').css("display", "");
                    $('#latiRange').text("取值范围：" + value.minValue + "~" + value.maxValue);
                    $("#latMinVal").val(value.minValue);
                    $("#latMaxVal").val(value.maxValue);
                    elementsdb.push(value.elementZh + ";" + value.elementEn);
                }
                if (value.isOutParameter == 0 && value.elementZh == "高度" || value.elementZh == "深度") {
                    // var realValues = value.realValue.split(",");
                    // for (var j = 0; j < realValues.length; j++) {
                    //     var realValue = realValues[j];
                    //     $("#depth").append("<option value='" + realValue + "'>" + realValue + "</option>");
                    // }
                    var range = value.minValue + "," + value.maxValue;
                    $('#depthSlider').jRange('updateRange', range, range);
                    $('#depthDiv').css("display", "");
                    $('#depthRange').text("取值范围：" + value.minValue + "~" + value.maxValue);
                    $("#depthMinVal").val(value.minValue);
                    $("#depthMaxVal").val(value.maxValue);
                    elementsdb.push(value.elementZh + ";" + value.elementEn);
                }
            }
        }
    });
}*/
function isLogin() {
    var loginName = "[[${@permission.getPrincipalPropertyUnLogged('loginName')}]]";
if (loginName){
    return true
}else {
    return false
}
}
function createWork() {
    if (!isLogin()) {
        showLoginDiv();
        return;
    }
    var workType = $("#workType").val();
    var productId = $("#productId").val();
    var beginDate = vue.value2[0];//$("#beginDate").val();
    var endDate = vue.value2[1]//$("#endDate").val();
    var nowDate = getNowDate();

    if (beginDate > endDate) {
        $.modal.alertError("开始时间不能小于结束时间");
        return;
    }
    if(beginDate == "" && endDate == ""){
        $.modal.alertError("开始时间和结束时间不能为空");
        return;
    }
    //var elementDtos = new Array();
    console.log("点击订阅")
    if (workType == "1") {
        //海洋站数据
       /* var createWorkItemDto = new Object();
        for (var i in elementsdb) {
            var elements = elementsdb[i].split(";");
            var element = elements[0];
            var elementEn = elements[1];
            if (element == "高度" || element == "深度") {
                var depth = $("#depthSlider").jRange('getValue').split(',');
                elementDto = {
                    element: elementEn,
                    minValue: $("#depthMinVal").val(),
                    maxValue: $("#depthMaxVal").val(),
                    flag: "-1"
                };
            }
            if (element == "纬度") {
                var lati = $("#latiSlider").jRange('getValue').split(',');
                elementDto = {
                    element: elementEn,
                    minValue: $("#latMinVal").val(),
                    maxValue: $("#latMinVal").val(),
                    flag: '1'
                };
            }
            if (element == "经度") {
                var long = $("#longSlider").jRange('getValue').split(',');
                elementDto = {
                    element: elementEn,
                    minValue: $("#longMinVal").val(),
                    maxValue: $("#longMaxVal").val(),
                    flag: '0'
                };
            }
            elementDtos.push(elementDto);
        }*/
    var oWorkOrdernccj = {
        minLon : $("#longMinVal").val(),//左下角x坐标
        maxLon : $("#longMaxVal").val(),//右上角x坐标
        minLat : $("#latMinVal").val(),//左下角y坐标
        maxLat : $("#latMaxVal").val(),//右上角y坐标
    }
    }
    console.log("点击订阅")
    var data = {
        "productId": productId,
        "beginDate": beginDate,
        "endDate": endDate,
        "workType": workType,
        "oWorkOrdernccj": oWorkOrdernccj
    };
    console.log(data)
    $.ajax({
        type: "post",
        async: true,
        url: "/home/productBrowsing/createWork",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(data),
        dataType: "json",        //返回数据形式为json
        success: function (result) {
            if (result.code == 0) {
                $.modal.alertSuccess(result.msg)
                $("#test").load("/home/productBrowsing/browseHP")
            } else {
                $.modal.alertError(result.msg);
            }
        }
    });
}

function getNowDate() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    if (month < 10) {
        month = '0' + month;
    }
    var day = now.getDate();
    if (day < 10) {
        day = "0" + day;
    }
    var date = year + '/' + month + '/' + day;
    return date;
}

var reg = "^[\\+\\-]?[\\d]+(\\.[\\d]+)?$";
var re = new RegExp(reg);
$("#longMinVal").change(function () {
    var value = $('#longSlider').jRange('getRange').split(",");
    if (!re.test($("#longMinVal").val())) {
        $("#longMinVal").val(value[0]);
        alert("请输入正确格式");
        return;
    }
    if (parseFloat($("#longMinVal").val()) < parseFloat(value[0]) || parseFloat($("#longMinVal").val()) > parseFloat($("#longMaxVal").val())) {
        $("#longMinVal").val(value[0]);
        alert("请输入正确值");
        return;
    }
    var value = $('#longSlider').jRange('getRange').split(",");
    var range = $("#longMinVal").val() + "," + $("#longMaxVal").val();
    $('#longSlider').jRange('setValue', range);
});

$("#longMaxVal").change(function () {
    var value = $('#longSlider').jRange('getRange').split(",");
    if (!re.test($("#longMaxVal").val())) {
        $("#longMaxVal").val(value[1]);
        alert("请输入正确格式");
        return;
    }
    if (parseFloat($("#longMaxVal").val()) > parseFloat(value[1]) || parseFloat($("#longMinVal").val()) > parseFloat($("#longMaxVal").val())) {
        $("#longMaxVal").val(value[1]);
        alert("请输入正确值");
        return;
    }
    var value = $('#longSlider').jRange('getRange').split(",");
    var range = $("#longMinVal").val() + "," + $("#longMaxVal").val();
    $('#longSlider').jRange('setValue', range);
});

$("#latMinVal").change(function () {
    var value = $('#latiSlider').jRange('getRange').split(",");
    if (!re.test($("#latMinVal").val())) {
        $("#latMinVal").val(value[0]);
        alert("请输入正确格式");
        return;
    }
    if (parseFloat($("#latMinVal").val()) < parseFloat(value[0]) || parseFloat($("#latMinVal").val()) > parseFloat($("#latMaxVal").val())) {
        $("#latMinVal").val(value[0]);
        alert("请输入正确值");
        return;
    }
    var value = $('#latiSlider').jRange('getRange').split(",");
    var range = $("#latMinVal").val() + "," + $("#latMaxVal").val();
    $('#latiSlider').jRange('setValue', range);
});

$("#latMaxVal").change(function () {
    var value = $('#latiSlider').jRange('getRange').split(",");
    if (!re.test($("#latMaxVal").val())) {
        $("#latMaxVal").val(value[1]);
        alert("请输入正确格式");
        return;
    }
    if (parseFloat($("#latMaxVal").val()) > parseFloat(value[1]) || parseFloat($("#latMinVal").val()) > parseFloat($("#latMaxVal").val())) {
        $("#latMaxVal").val(value[1]);
        alert("请输入正确值");
        return;
    }
    var value = $('#latiSlider').jRange('getRange').split(",");
    var range = $("#latMinVal").val() + "," + $("#latMaxVal").val();
    $('#latiSlider').jRange('setValue', range);
});

$("#depthMinVal").change(function () {
    var value = $('#depthSlider').jRange('getRange').split(",");
    if (!re.test($("#depthMinVal").val())) {
        $("#depthMinVal").val(value[0]);
        alert("请输入正确格式");
        return;
    }
    if (parseFloat($("#depthMinVal").val()) < parseFloat(value[0]) || parseFloat($("#depthMinVal").val()) > parseFloat($("#depthMinVal").val())) {
        $("#depthMinVal").val(value[0]);
        alert("请输入正确值");
        return;
    }
    var value = $('#depthSlider').jRange('getRange').split(",");
    var range = $("#depthMinVal").val() + "," + $("#depthMaxVal").val();
    $('#depthSlider').jRange('setValue', range);
});

$("#depthMaxVal").change(function () {
    var value = $('#depthSlider').jRange('getRange').split(",");
    if (!re.test($("#depthMaxVal").val())) {
        $("#depthMaxVal").val(value[1]);
        alert("请输入正确格式");
        return;
    }
    if (parseFloat($("#depthMaxVal").val()) > parseFloat(value[1]) || parseFloat($("#depthMaxVal").val()) > parseFloat($("#depthMaxVal").val())) {
        $("#depthMaxVal").val(value[1]);
        alert("请输入正确值");
        return;
    }
    var value = $('#depthSlider').jRange('getRange').split(",");
    var range = $("#depthMinVal").val() + "," + $("#depthMaxVal").val();
    $('#depthSlider').jRange('setValue', range);
});
function sliderChange() {
    $('#longSlider').change(function () {
        var value = $('#longSlider').jRange('getValue').split(",");
        $("#longMinVal").val(value[0]);
        $("#longMaxVal").val(value[1]);
    });
    $('#latiSlider').change(function () {
        var value = $('#latiSlider').jRange('getValue').split(",");
        $("#latMinVal").val(value[0]);
        $("#latMaxVal").val(value[1]);
    });
    $('#depthSlider').change(function () {
        var value = $('#depthSlider').jRange('getValue').split(",");
        $("#depthMinVal").val(value[0]);
        $("#depthMaxVal").val(value[1]);
    });
}

function renewalProduct(){
    if (!isLogin()) {
        showLoginDiv();
        return;
    }
    var productId = $("#productId").val();
    var renewalDays = $("#renewalDays").val();
    var workType = $("#workType").val();
    var data = {
        "productId": productId,
        "renewalDays": renewalDays,
        "workType": workType,
    };
    $.ajax({
        type: "post",
        async: true,
        url: "/home/productBrowsing/renewalProduct",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(data),
        dataType: "json",        //返回数据形式为json
        success: function (result) {
            if (result.code == web_status.SUCCESS) {
                alert(result.msg);
            } else {
                alert(result.msg);
            }
        }
    });
}
